/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */

package cn.knet.sp.ncrm.modules.sys.utils;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.utils.CacheUtils;
import cn.knet.sp.ncrm.common.utils.DateUtils;
import cn.knet.sp.ncrm.common.utils.SpringContextHolder;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.modules.sys.region.dao.RegionDao;
import cn.knet.sp.ncrm.modules.sys.region.entity.Region;
import cn.knet.sp.ncrm.modules.sys.user.entity.User;
//import com.cintel.ws.cc.wsdl.v1_0.agent._interface.AgentService;
//import com.cintel.ws.cc.wsdl.v1_0.agent.model.AgentContext;
//import com.cintel.ws.cc.wsdl.v1_0.agent.model.AgentFuncConfig;
//import com.cintel.ws.cc.wsdl.v1_0.agent.model.AgentInfo;
//import com.cintel.ws.cc.wsdl.v1_0.agent.service.AgentWsLocator;
//import com.cintel.ws.cc.wsdl.v1_0.extension._interface.ExtensionService;
//import com.cintel.ws.cc.wsdl.v1_0.extension.model.ConferenceType;
//import com.cintel.ws.cc.wsdl.v1_0.extension.model.ExtensionContext;
//import com.cintel.ws.cc.wsdl.v1_0.extension.model.ExtensionType;
//import com.cintel.ws.cc.wsdl.v1_0.extension.service.ExtensionWsLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.List;

/**
 * 呼叫中心工具类(WebService调用)
 * @author shishouyang
 * @version 2013-09-17
 */
public class CallUtils {

	/**
	 * 日志对象
	 */
	protected static Logger logger = LoggerFactory.getLogger(CallUtils.class);
	static String vccid = Global.getConfig("call.server.vccid");
	static String serviceIp = Global.getConfig("call.server.default");
	static String servicePort = Global.getConfig("call.server.webservice.port");
	static String agentDn = Global.getConfig("call.server.agent.dn");
	static String agentHean = Global.getConfig("call.server.agent.head");
	static String agentPasswd = Global.getConfig("call.server.agent.passwd");

	/**
	 * 新增或修改用户时,同时新增或修改分机和坐席
	 * 2015-08-10
	 * @author shishouyang
	 */
	public static void createCallCenterNo(User user, RedirectAttributes redirectAttributes)
			throws ServiceException, MalformedURLException, RemoteException {
		
//		if(null!=user && StringUtils.isNotBlank(user.getNo())) {
//			if (user.getNo().matches("^[1-9][0-9]{1,4}$")) {
//				/**
//				 * 添加分机
//				 */
//				String extensionEndPoint = "http://" + serviceIp + ":" + servicePort + "/cin-cc-ws-v2/services/ExtensionService";
//				String sipServerName = "SipServer";
//				ExtensionService esp = new ExtensionWsLocator().getExtensionService(new URL(extensionEndPoint));
//				ExtensionType et = ExtensionType.fromString("AGENT");
//				ConferenceType ct = ConferenceType.fromString("AudioConf");
//				ExtensionContext extensionContext = new ExtensionContext(vccid, agentDn + vccid + user.getNo(), et, "", 1, "1", 0, ct, sipServerName, "", user.getNo(), user.getNo(), user.getNo(), user.getNo(), 0, 0, -1, -1, 1);
//				//esp.save(extensionContext);
//				//esp.update(extensionContext);
//				//esp.delete(agentDn + vccid + userNo);
//				ExtensionContext context = null;
//				try {
//					context = esp.find(agentDn + vccid + user.getNo());
//				} catch (RemoteException e) {
//					//e.printStackTrace();
//					logger.warn("没有找到该话机号"+user.getNo());
//				}
//
//				if (null != context) {
//					logger.info("该分机已存在,更新分机");
//					esp.update(extensionContext);
//				} else {
//					logger.info("该分机不存在,新增分机");
//					esp.save(extensionContext);
//				}
//
//				/**
//				 * 添加坐席
//				 */
//				String endPoint = "http://" + serviceIp + ":" + servicePort + "/cin-cc-ws-v2/services/AgentService";
//				AgentService as = new AgentWsLocator().getAgentService(new URL(endPoint));
//				AgentContext ac = new AgentContext(vccid, agentHean + vccid + user.getNo(), user.getName(), 0, 0, agentPasswd, DateUtils
//						.timeStampAllStr(), DateUtils.timeStampAllStr(), 0, agentDn + vccid + user.getNo(), Integer.valueOf(1), "", 0, 1, 2, 3, 8, 1, 1, 1, 0, 1, "", 1, 0, "");
//				AgentFuncConfig afc = new AgentFuncConfig(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
//				AgentInfo agentInfo = new AgentInfo(ac, afc);
//				//as.save(agentInfo);
//				//as.update(agentInfo);
//				//as.delete(agentHean + vccid + userNo);
//				AgentInfo info = null;
//				try {
//					info = as.find(agentHean + vccid + user.getNo());
//				} catch (RemoteException e) {
//					logger.warn("没有找到该坐席"+user.getNo());
//				}
//				if (null != info) {
//					logger.info("该坐席已存在,更新坐席");
//					as.update(agentInfo);
//				} else {
//					logger.info("该坐席不存在,新增坐席");
//					as.save(agentInfo);
//				}
//
//			} else {
//				// 清除当前用户缓存
//				if (user.getLoginName().equals(UserUtils.getUser().getLoginName())) {
//					UserUtils.clearCache();
//					//UserUtils.getCacheMap().clear();
//				}
//				addMessage(redirectAttributes, "保存用户'" + user.getLoginName() + "'失败,工号必须为数字,并且长度为1至8位");
//			}
//		}
	}

	/**
	 * 删除用户时,将坐席名称重置为空
	 * 2015-09-17
	 * @author shishouyang
	 */
	public static void resetAgentName(User user, RedirectAttributes redirectAttributes) throws MalformedURLException, ServiceException, RemoteException {
		/**
		 * 添加坐席
		 */
//		String endPoint = "http://" + serviceIp + ":" + servicePort + "/cin-cc-ws-v2/services/AgentService";
//		AgentService as = new AgentWsLocator().getAgentService(new URL(endPoint));
//		AgentContext ac = new AgentContext(vccid, agentHean + vccid + user.getNo(), "", 0, 0, agentPasswd, DateUtils
//				.timeStampAllStr(), DateUtils.timeStampAllStr(), 0, agentDn + vccid + user.getNo(), Integer.valueOf(1), "", 0, 1, 2, 3, 8, 1, 1, 1, 0, 1, "", 1, 0, "");
//		AgentFuncConfig afc = new AgentFuncConfig(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
//		AgentInfo agentInfo = new AgentInfo(ac, afc);
//		//as.save(agentInfo);
//		//as.update(agentInfo);
//		//as.delete(agentHean + vccid + userNo);
//		AgentInfo info = null;
//		try {
//			info = as.find(agentHean + vccid + user.getNo());
//		} catch (RemoteException e) {
//			logger.warn("没有找到该坐席"+user.getNo());
//		}
//		if (null != info) {
//			logger.info("该坐席已存在,更新坐席");
//			as.update(agentInfo);
//		}
	}

	/**
	 * 添加Flash消息
	 * @param messages
	 * 2015-09-17
	 * @author shishouyang
	 */
	protected static void addMessage(RedirectAttributes redirectAttributes, String... messages) {
		StringBuilder sb = new StringBuilder();
		for (String message : messages){
			sb.append(message).append(messages.length>1?"<br/>":"");
		}
		redirectAttributes.addFlashAttribute("message", sb.toString());
	}

}
